package com.statistics.service.Impl;

import com.statistics.cloudpower.dao.TblElectricityStatisticsDayDAO;
import com.statistics.cloudpower.dao.TblElectricityStatisticsMonthDAO;
import com.statistics.cloudpower.dao.TblElectricityStatisticsWeekDAO;
import com.statistics.cloudpower.dao.TblEquipmentDAO;
import com.statistics.cloudpower.entity.TblEquipment;
import com.statistics.service.StatisticsService;
import com.statistics.thread.ElectricityStatisticsThread;
import com.statistics.tools.Constants;
import com.statistics.yfdata.dao.DataElectricityDAO;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;
import java.util.UUID;

/**
 * @author MәӧωρaЯsε
 * @date 2017/7/11.
 */
@Service
public class StatisticsServiceImpl implements StatisticsService{

    private int day_size = 30;
    private int week_size = 30;
    private int month_size = 30;

    private final org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private TblElectricityStatisticsDayDAO tblElectricityStatisticsDayDAO;
    @Autowired
    private TblElectricityStatisticsWeekDAO tblElectricityStatisticsWeekDAO;
    @Autowired
    private TblElectricityStatisticsMonthDAO tblElectricityStatisticsMonthDAO;
    @Autowired
    private DataElectricityDAO dataElectricityDAO;
    @Autowired
    private TblEquipmentDAO tblEquipmentDAO;

    @Override
    public void countEveryDay() throws ParseException {
        Long equipment_count = tblEquipmentDAO.count(Constants.ELECTRICITY_TYPE);
        String uuid = UUID.randomUUID().toString();
        int fragment_number = 1;

        for (long start = 0;start < equipment_count;start+=day_size,fragment_number++){
            //读取count个设备
            List<TblEquipment> tblEquipment = tblEquipmentDAO.findPageBySql(Constants.ELECTRICITY_TYPE,start,day_size);
            //command为任务模式-每日、每周、每月
            new ElectricityStatisticsThread(Constants.EVERY_DAY,dataElectricityDAO,tblEquipment,tblElectricityStatisticsDayDAO,uuid,fragment_number).start();
        }
    }

    @Override
    public void countEveryWeek() throws ParseException {

        Long equipment_count = tblEquipmentDAO.count(Constants.ELECTRICITY_TYPE);
        String uuid = UUID.randomUUID().toString();
        int fragment_number = 1;

        for (long start = 0;start < equipment_count;start+=week_size,fragment_number++){

            //读取count个设备
            List<TblEquipment> tblEquipment = tblEquipmentDAO.findPageBySql(Constants.ELECTRICITY_TYPE,start,week_size);

            //command为任务模式-每日、每周、每月
            new ElectricityStatisticsThread(Constants.EVERY_WEEK,
                    dataElectricityDAO,
                    tblEquipment,tblElectricityStatisticsWeekDAO,uuid,fragment_number).start();
        }
    }

    @Override
    public void countEveryMonth() {
        Long equipment_count = tblEquipmentDAO.count(Constants.ELECTRICITY_TYPE);
        String uuid = UUID.randomUUID().toString();
        int fragment_number = 1;

        for (long start = 0;start < equipment_count;start+=month_size,fragment_number++){

            //读取count个设备
            List<TblEquipment> tblEquipment = tblEquipmentDAO.findPageBySql(Constants.ELECTRICITY_TYPE,start,month_size);

            //command为任务模式-每日、每周、每月
            new ElectricityStatisticsThread(Constants.EVERY_MONTH,
                    dataElectricityDAO,
                    tblEquipment,
                    tblElectricityStatisticsMonthDAO,uuid,fragment_number).start();
        }
    }
}
